﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KnitwearCompany.Models;
using KnitwearCompany.Managers;

namespace KnitwearCompany.Controllers
{
    public class AccountController : Controller
    {
        private Manager manager = new Manager();

        public ActionResult Login()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Login([Bind] LogOnModel model)
        {
            if (ModelState.IsValid)
            {
                var user = manager.GetUser(model.UserName);
                if (user != null && user.Password == model.Password)
                {
                    Session["User"] = user;
                    if (Session["Cart"] == null)
                        Session["Cart"] = new Cart();
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "Логин или пароль неверны.");
                }
            }
            return View(model);
        }

        public ActionResult Logout()
        {
            Session["User"] = null;
            Session["Cart"] = null;
            return RedirectToAction("Index", "Home");
        }

        public ActionResult Register()
        {
            ViewData["PasswordLength"] = 3;
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register([Bind] RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new User();
                user.Login = model.UserName;
                user.Password = model.Password;
                user.IsAdmin = false;
                if (model.Password.Equals(model.ConfirmPassword) && manager.AddUser(user))
                {
                    Session["User"] = manager.GetUser(user.Login);
                    Session["Cart"] = new Cart();
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "Произошла ошибка");
                }
            }

            // If we got this far, something failed, redisplay form
            ViewData["PasswordLength"] = 3;
            return View(model);
        }

        public ActionResult EditUserRole(int id)
        {
            var user = manager.GetUser(id);
            user.IsAdmin = !user.IsAdmin;
            manager.UpdateUser(user);
            if (((User)Session["User"]).Id == user.Id)
                Session["User"] = user;
            return RedirectToAction("Users");
        }

        public ActionResult Users()
        {
            return View(manager.GetUsers());
        }
                    
        public ActionResult RemoveUser(int id)
        {
            manager.DeleteUser(id);
            return RedirectToAction("Users");
        }

    }
}
